//==============================================================================
//
//  File        : pCam_usb.c
//  Description : Called by UVC,UAC
//
//==============================================================================
#include "includes_fw.h"
#include "lib_retina.h"
#include "mmpf_typedef.h"
#include "mmp_register.h"
#include "mmp_reg_gpio.h"
#include "mmp_reg_gbl.h"

#include "mmpf_usbuvc.h"
#include "pCam_usb.h"
#include "pCam_msg.h"
#include "mmps_3gprecd.h"
#include "pcam_handler.h"
#include "mmpf_i2cm.h"
//==============================================================================
//
//                              Define
//
//==============================================================================
// ----------------------------------------------------
#include "mmpf_mp4venc.h"
MMP_UBYTE gsUSBXU_I2C_ID ;
MMP_BOOL  gbBayerRawPreviewEn = 0;

//==============================================================================
//
//                              External
//
//==============================================================================

//==============================================================================
//
//                              FUNCTION PROTOTYPES
//
//==============================================================================
MMP_BOOL USB_IsPreviewActive(void)
{   
    MMP_BOOL en ;
    MMPS_3GPRECD_GetPreviewDisplay(&en) ;
    return en; 
}

MMP_USHORT USB_SetAsyncVideoControl(MMP_UBYTE bOriginator,MMP_UBYTE bSelector,MMP_UBYTE bAttribute,MMP_UBYTE bValUnit)
{
    PCAM_SetAsyncVideoControl(bOriginator, bSelector, bAttribute, bValUnit);    
    return 0;
}


/*
UVC/UAC -> pCam API
*/

// UVC preview start
MMP_USHORT USB_VideoPreviewStart(MMP_USHORT nonblocking)
{
    MMP_USHORT err = PCAM_ERROR_NONE ;
    pcam_usb_set_nonblocking(nonblocking);
    pcam_usb_preview();
    return err ;
}

extern MMP_UBYTE XU_Ctrl_StreamID;
MMP_USHORT USB_ForceH264IDRFrame(void)
{
    MMPF_VIDENC_SetParameter(XU_Ctrl_StreamID, MMPF_VIDENC_ATTRIBUTE_FORCE_I, (void*)MMPF_VIDENC_PICCTL_IDR_RESYNC);   
#if USB_FRAMEBASE_DUAL_H264_STREAM
    if(XU_Ctrl_StreamID==0xFF)
    MMPF_VIDENC_SetParameter(1, MMPF_VIDENC_ATTRIBUTE_FORCE_I, (void*)MMPF_VIDENC_PICCTL_IDR_RESYNC);
#endif
    return PCAM_ERROR_NONE;
}

// UVC preview stop
MMP_USHORT USB_VideoPreviewStop(MMP_USHORT nonblocking)
{
    MMP_USHORT err = PCAM_ERROR_NONE ;
    // sean@2010_12_28, alyways stop bayer raw preview when stop
    USB_EnableBayerRawPreview(0);
    pcam_usb_set_nonblocking(nonblocking);
    err = pcam_usb_preview_stop();
    return err;
}

// UVC power down
MMP_USHORT USB_VideoPowerDown(MMP_USHORT nonblocking)
{
    MMP_USHORT err = PCAM_ERROR_NONE ;
    pcam_usb_set_nonblocking(nonblocking);
    err = pcam_usb_exit();
    return err;
}

#if SUPPORT_UAC
// UAC audio start
MMP_USHORT USB_AudioReset(MMP_USHORT nonblocking)
{
    MMP_USHORT err = PCAM_ERROR_NONE ;
    pcam_usb_set_nonblocking(nonblocking);
    err = pcam_usb_audio_reset();
    return err ;
}

// UAC audio start
MMP_USHORT USB_AudioPreviewStart(MMP_USHORT nonblocking)
{
    MMP_USHORT err = PCAM_ERROR_NONE ;
    pcam_usb_set_nonblocking(nonblocking);
    err = pcam_usb_audio_start();
    return err ;
}

// UAC audio stop
MMP_USHORT USB_AudioPreviewStop(MMP_USHORT nonblocking)
{
    MMP_USHORT err = PCAM_ERROR_NONE ;
    pcam_usb_set_nonblocking(nonblocking);
    err = pcam_usb_audio_stop();
    return err ;
}


MMP_USHORT USB_AudioSetSampleRate(MMP_USHORT nonblocking,MMP_ULONG sample_rate)
{
    pcam_usb_set_nonblocking(nonblocking);
    return pcam_usb_set_samplerate(sample_rate);
}


// 1 : Mute , 0 : UnMute
MMP_USHORT USB_AudioSetMute(MMP_USHORT nonblocking,MMP_BOOL mute)
{
    //RTNA_DBG_Str3("USB_AudioSetMute\r\n");
    pcam_usb_set_nonblocking(nonblocking);
    return pcam_usb_set_mute(mute);
}

// vol : UAC unit
MMP_USHORT USB_AudioSetVolume(MMP_USHORT nonblocking,MMP_SHORT vol)
{
    //RTNA_DBG_Str3("USB_AudioSetVolume\r\n");
    pcam_usb_set_nonblocking(nonblocking);
    return pcam_usb_set_volume(vol);
    
}
#endif  // #if SUPPORT_UAC

// UVC set saturation
MMP_USHORT USB_SetSaturation(MMP_USHORT nonblocking,MMP_USHORT val)
{
    pcam_usb_set_nonblocking(nonblocking);
    return pcam_usb_set_attributes(PCAM_USB_SETTING_SATURATION,&val);
}

// UVC set contrast
MMP_USHORT USB_SetContrast(MMP_USHORT nonblocking,MMP_USHORT val)
{
    pcam_usb_set_nonblocking(nonblocking);
    return pcam_usb_set_attributes(PCAM_USB_SETTING_CONTRAST,&val);
}

// UVC set brightness
MMP_USHORT USB_SetBrightness(MMP_USHORT nonblocking,MMP_SHORT val)
{
    pcam_usb_set_nonblocking(nonblocking);
    return pcam_usb_set_attributes(PCAM_USB_SETTING_BRIGHTNESS,&val);
}

// UVC set hue
MMP_USHORT USB_SetHue(MMP_USHORT nonblocking,MMP_SHORT val)
{
    pcam_usb_set_nonblocking(nonblocking);
    return pcam_usb_set_attributes(PCAM_USB_SETTING_HUE,&val);
}

// UVC set gamma
MMP_USHORT USB_SetGamma(MMP_USHORT nonblocking,MMP_USHORT val)
{
    pcam_usb_set_nonblocking(nonblocking);
    return pcam_usb_set_attributes(PCAM_USB_SETTING_GAMMA,&val);
}

// UVC set backlight
MMP_USHORT USB_SetBacklight(MMP_USHORT nonblocking,MMP_USHORT val)
{
    pcam_usb_set_nonblocking(nonblocking);
    return pcam_usb_set_attributes(PCAM_USB_SETTING_BACKLIGHT,&val);
}

// UVC set sharpness
MMP_USHORT USB_SetSharpness(MMP_USHORT nonblocking,MMP_USHORT val)
{
    pcam_usb_set_nonblocking(nonblocking);
    return pcam_usb_set_attributes(PCAM_USB_SETTING_SHARPNESS,&val);
}

// UVC set gain
MMP_USHORT USB_SetGain(MMP_USHORT nonblocking,MMP_USHORT val)
{
    pcam_usb_set_nonblocking(nonblocking);
    return pcam_usb_set_attributes(PCAM_USB_SETTING_GAIN,&val);
}

// UVC set lens position
MMP_USHORT USB_SetLensPosition(MMP_USHORT nonblocking,MMP_USHORT val)
{
    pcam_usb_set_nonblocking(nonblocking);
    return pcam_usb_set_attributes(PCAM_USB_SETTING_LENS,&val);
}

// UVC auto focus
MMP_USHORT USB_EnableAF(MMP_USHORT nonblocking,MMP_BOOL val)
{
    pcam_usb_set_nonblocking(nonblocking);
    return pcam_usb_set_attributes(PCAM_USB_SETTING_AF,&val);
}

// UVC enable AE
MMP_USHORT USB_EnableAE(MMP_USHORT nonblocking,MMP_BOOL val)
{
    pcam_usb_set_nonblocking(nonblocking);
    return pcam_usb_set_attributes(PCAM_USB_SETTING_AE,&val);
}

// UVC set AE exposure time
MMP_USHORT USB_SetAEExposureValue(MMP_USHORT nonblocking,MMP_ULONG val)
{
    pcam_usb_set_nonblocking(nonblocking);
    return pcam_usb_set_attributes(PCAM_USB_SETTING_EV,&val);
}

// UVC set awb mode
MMP_USHORT USB_SetAWBMode(MMP_USHORT nonblocking,MMP_UBYTE val)
{
    pcam_usb_set_nonblocking(nonblocking);
    return pcam_usb_set_attributes(PCAM_USB_SETTING_WB,&val);
}

// UVC set awb temp
MMP_USHORT USB_SetAWBTemp(MMP_USHORT nonblocking,MMP_USHORT  val)
{
    pcam_usb_set_nonblocking(nonblocking);
    return pcam_usb_set_attributes(PCAM_USB_SETTING_WB_TEMP,&val);
}
//
// UVC set digital zoom
//
#if 0
MMP_USHORT USB_SetDigitalZoom(MMP_USHORT nonblocking,MMP_UBYTE dir,MMP_UBYTE range_min,MMP_UBYTE range_max,MMP_UBYTE range_step)
{
    PCAM_USB_ZOOM val ;
    //RTNA_DBG_Str(3,"USB_SetDigitalZoom\r\n");
    
    val.Dir = dir ;
    val.RangeMin = range_min ;
    val.RangeMax = range_max ;
    val.RangeStep = range_step;
    pcam_usb_set_nonblocking(nonblocking);
    return pcam_usb_set_attributes(PCAM_USB_SETTING_DIGZOOM,&val);
}
#endif


//
// UVC set digital pan & Tilt
//
MMP_USHORT USB_SetDigitalPanTilt(MMP_USHORT nonblocking,MMP_LONG pan_min,MMP_LONG pan_max,MMP_LONG tilt_min,MMP_LONG tilt_max,MMP_USHORT total_step)
{
    PCAM_USB_PANTILT val ;
    //RTNA_DBG_Str(3,"USB_SetDigitalPanTilt\r\n");
    
    val.PanMin  = pan_min  ;
    val.PanMax  = pan_max;
    val.TiltMin = tilt_min  ;
    val.TiltMax = tilt_max;
    val.Steps = total_step ;
    
    pcam_usb_set_nonblocking(nonblocking);
    return pcam_usb_set_attributes(PCAM_USB_SETTING_DIGPAN,&val);

}
// UVC set line freq ( 50/60 debanding )
MMP_USHORT USB_SetPowerLineFreq(MMP_USHORT nonblocking,MMP_UBYTE val)
{
    PCAM_USB_DEBAND deband = PCAM_USB_DEBAND_60HZ ;
    if(val==BAND_50HZ) {
       deband = PCAM_USB_DEBAND_50HZ ; 
    }   
    if(val==BAND_DISABLE) {
       deband = PCAM_USB_DEBAND_AUTO ; 
    }
    if(val==BAND_AUTO_LE) {
       deband = PCAM_USB_DEBAND_AUTO_LIMITEXPO ; 
    }
    if(val==BAND_50HZ_LE) {
       deband = PCAM_USB_DEBAND_50HZ_LIMITEXPO ; 
    }
    if(val==BAND_60HZ_LE) {
       deband = PCAM_USB_DEBAND_60HZ_LIMITEXPO ; 
    }
    pcam_usb_set_nonblocking(nonblocking);
    return pcam_usb_set_attributes(PCAM_USB_SETTING_DEBAND,&deband);
}

// UVC set H264(only) resolution
MMP_USHORT USB_SetH264Resolution(MMP_USHORT nonblocking,PCAM_USB_VIDEO_RES pCamRes)
{
    MMP_USHORT err = PCAM_ERROR_NONE ;    
   // MMP_BOOL IsPreviewEnable;

    pcam_usb_set_nonblocking(nonblocking);
 //   MMPS_3GPRECD_GetPreviewDisplay(&IsPreviewEnable);
 //   if(IsPreviewEnable==0) {
        err = pcam_usb_set_attributes(PCAM_USB_SETTING_H264_RES,&pCamRes);
//    }
    return err;
}

// UVC set video format
MMP_USHORT USB_SetVideoFormat(MMP_USHORT nonblocking,PCAM_USB_VIDEO_FORMAT pCamVidFmt)
{
    MMP_USHORT err = PCAM_ERROR_NONE ;    
    //MMP_BOOL IsPreviewEnable;

    pcam_usb_set_nonblocking(nonblocking);
//    MMPS_3GPRECD_GetPreviewDisplay(&IsPreviewEnable);
//    if(IsPreviewEnable==0) {
        err = pcam_usb_set_attributes(PCAM_USB_SETTING_VIDEO_FORMAT,&pCamVidFmt);
//    }
    return err;
}

#if 0
// UVC set video quality
MMP_USHORT USB_SetVideoQuality(MMP_USHORT nonblocking,PCAM_USB_VIDEO_QUALITY pCamQ)
{
    pcam_usb_set_nonblocking(nonblocking);
    return pcam_usb_set_attributes(PCAM_USB_SETTING_VIDEO_QUALITY,&pCamQ);
}
#endif

// UVC set video resolution
MMP_USHORT USB_SetVideoResolution(MMP_USHORT nonblocking,PCAM_USB_VIDEO_RES pCamRes)
{
    pcam_usb_set_nonblocking(nonblocking);
    return pcam_usb_set_attributes(PCAM_USB_SETTING_VIDEO_RES,&pCamRes);
}

void USB_LEDDisplay(MMP_UBYTE num, MMP_UBYTE status)
{
    AITPS_GPIO  pGPIO = AITC_BASE_GPIO;

    if(num < 32){
#if ((CUSTOMER == CCN) && (MODEL == 1)) 
        if(status == LED_ON) {
            pGPIO->GPIO_DATA[0] &= ~(1 << num);
        } else {
            pGPIO->GPIO_DATA[0] |= (1 << num);
        }
#else
        if(status == LED_ON) {
            pGPIO->GPIO_DATA[0] |= (1 << num);
        } else {
            pGPIO->GPIO_DATA[0] &= ~(1 << num);
        }
#endif
    }
    else if(num < 64){

        if(status == LED_ON)
            pGPIO->GPIO_DATA[1] |= (1 << (num-32));  
        else
            pGPIO->GPIO_DATA[1] &= ~(1 << (num-32));  
    }
     
}

MMP_USHORT USB_UpdateFirmware(MMP_USHORT nonblocking,MMP_USHORT step) 
{
    pcam_usb_set_nonblocking(nonblocking);
    return pcam_usb_update_firmware(step);

}

#if SUPPORT_UAC
MMP_USHORT USB_SetAudioMode(MMP_USHORT nonblocking,MMP_UBYTE mode) 
{
    pcam_usb_set_nonblocking(nonblocking);
    return pcam_usb_audio_set_mode(mode);

}
#endif

MMP_USHORT USB_CheckTaskAlive(MMP_USHORT heartbeat)
{
    pcam_usb_set_nonblocking(PCAM_NONBLOCKING);
    return pcam_usb_task_alive(heartbeat);
    
}

MMP_USHORT USB_TakeRawPicture(MMP_USHORT nonblocking,MMP_ULONG addr) 
{
    pcam_usb_set_nonblocking(nonblocking);
    return pcam_usb_take_rawpicture(addr);

}

#if 1
static MMPF_I2CM_ATTRIBUTE* _USB_MakeI2cAttribute(MMP_UBYTE mode)
{
    AITPS_GBL   pGBL = AITC_BASE_GBL;
#if (CUSTOMER == NKT)
    static MMPF_I2CM_ATTRIBUTE uI2cmAttribute =  {MMPF_I2CM_ID_0, 0x14, 16, 8, 0, MMP_FALSE, MMP_FALSE, MMP_FALSE, MMP_FALSE, 0, 0, 0, MMPF_I2CM_SPEED_HW_250K, NULL, NULL};
#elif (CUSTOMER == SAC) || (CUSTOMER == LIT) || ((CUSTOMER == CCN)&&(MODEL == 1)) || ((CUSTOMER == LGT)&&(MODEL == 1))
    static MMPF_I2CM_ATTRIBUTE uI2cmAttribute =  {MMPF_I2CM_ID_0, 0x14, 16, 8, 0, MMP_FALSE, MMP_FALSE, MMP_FALSE, MMP_FALSE, 0, 0, 3, MMPF_I2CM_SPEED_HW_250K, NULL, NULL};
#elif (CUSTOMER == ASU)
    static MMPF_I2CM_ATTRIBUTE uI2cmAttribute = {SENSOR_I2CM_ID, 0, 16, 8, 0, MMP_FALSE, MMP_FALSE, MMP_FALSE, MMP_FALSE, 0, 0, 0, MMPF_I2CM_SPEED_HW_250K, NULL, NULL};
#elif (CUSTOMER == ANW)
    static MMPF_I2CM_ATTRIBUTE uI2cmAttribute = {MMPF_I2CM_ID_0, 0x44, 8, 8, 0, MMP_FALSE, MMP_FALSE, MMP_FALSE, MMP_FALSE, 0, 0, 0, MMPF_I2CM_SPEED_HW_250K, NULL, NULL};
#else
    static MMPF_I2CM_ATTRIBUTE uI2cmAttribute =  {MMPF_I2CM_ID_0, 0, 8, 8, 0, MMP_FALSE, MMP_FALSE, MMP_FALSE, MMP_FALSE, 0, 0, 0, MMPF_I2CM_SPEED_HW_250K, NULL, NULL};
#endif
    uI2cmAttribute.ubSlaveAddr = gsUSBXU_I2C_ID;

    if ((pGBL->GBL_CLK_DIS0 & GBL_CLK_VI_DIS) || (pGBL->GBL_CLK_DIS1 & GBL_CLK_I2C_DIS)) {
        pGBL->GBL_CLK_DIS0 &= (~GBL_CLK_VI_DIS);
        pGBL->GBL_CLK_DIS1 &= (~GBL_CLK_I2C_DIS);//20110715@DongQ, for I2C read write at any time.
        MMPF_OS_Sleep_MS(3);
    }

    if(gsUSBXU_I2C_ID > 0){

        if(mode==I2C_MODE_2A1D){ //2A1D
            uI2cmAttribute.ubRegLen = 16;
            uI2cmAttribute.ubDataLen = 8;
        }
        else if(mode==I2C_MODE_1A1D) {//1A1D
            uI2cmAttribute.ubRegLen = 8;
            uI2cmAttribute.ubDataLen = 8;
        }
    } else {
        return 0;
    }
    return &uI2cmAttribute;
}
/*************************************************************************************************/
/* Write I2C Routines via UVC command                                                                     */
/*************************************************************************************************/
void USB_WriteI2C(MMP_USHORT addr, MMP_USHORT val,MMP_UBYTE mode)
{
    MMPF_I2CM_ATTRIBUTE* i2c_attr = _USB_MakeI2cAttribute(mode);

    if (i2c_attr != 0) {
        MMPF_I2cm_WriteReg(i2c_attr,addr, val);
    }
}

/*************************************************************************************************/
/* Read I2C Routines via UVC command                                                                     */
/*************************************************************************************************/
MMP_USHORT USB_ReadI2C(MMP_USHORT addr,MMP_UBYTE mode)
{
    MMP_USHORT ret = 0xFF;
    MMPF_I2CM_ATTRIBUTE* i2c_attr = _USB_MakeI2cAttribute(mode);

    if (i2c_attr != 0) {
        MMPF_I2cm_ReadReg(i2c_attr, addr, &ret);
    }

    return ret;
}
#else
/*************************************************************************************************/
/* Write I2C Routines via UVC command                                                                     */
/*************************************************************************************************/
void USB_WriteI2C(MMP_USHORT addr, MMP_USHORT val,MMP_UBYTE mode)
{
	MMP_USHORT I2CClockDiv = 0;
    AITPS_GBL   pGBL = AITC_BASE_GBL;
#if (CUSTOMER == NKT)
	MMPF_I2CM_ATTRIBUTE uI2cmAttribute =  {MMPF_I2CM_ID_0, 0x14, 16, 8, 0, MMP_FALSE, MMP_FALSE, MMP_FALSE, MMP_FALSE, 0, 0, 0, MMPF_I2CM_SPEED_HW_250K, NULL, NULL};
#elif (CUSTOMER == SAC) || (CUSTOMER == LIT) || ((CUSTOMER == CCN)&&(MODEL == 1)) || ((CUSTOMER == LGT)&&(MODEL == 1))
	MMPF_I2CM_ATTRIBUTE uI2cmAttribute =  {MMPF_I2CM_ID_0, 0x14, 16, 8, 0, MMP_FALSE, MMP_FALSE, MMP_FALSE, MMP_FALSE, 0, 0, 3, MMPF_I2CM_SPEED_HW_250K, NULL, NULL};
#elif  (CUSTOMER == ASU)
	MMPF_I2CM_ATTRIBUTE uI2cmAttribute = {SENSOR_I2CM_ID, 0, 16, 8, 0, MMP_FALSE, MMP_FALSE, MMP_FALSE, MMP_FALSE, 0, 0, 0, MMPF_I2CM_SPEED_HW_250K, NULL, NULL};
#else
	MMPF_I2CM_ATTRIBUTE uI2cmAttribute =  {MMPF_I2CM_ID_0, 0, 8, 8, 0, MMP_FALSE, MMP_FALSE, MMP_FALSE, MMP_FALSE, 0, 0, 0, MMPF_I2CM_SPEED_HW_250K, NULL, NULL};
#endif

	pGBL->GBL_CLK_DIS0 &= (~GBL_CLK_VI_DIS);
	pGBL->GBL_CLK_DIS1 &= (~GBL_CLK_I2C_DIS);//20110715@DongQ, for I2C read write at any time.
    MMPF_OS_Sleep_MS(1);

	I2CClockDiv = 200;//G0Clock/1000 * 512/380;
	uI2cmAttribute.ubSlaveAddr = gsUSBXU_I2C_ID;
	
	if((gsUSBXU_I2C_ID > 0) && ((pGBL->GBL_CLK_DIS0 & GBL_CLK_VI_DIS) == 0x0)){
		
		if(mode==0){ //2A1D
			uI2cmAttribute.ubRegLen = 16 ;
			uI2cmAttribute.ubDataLen = 8 ;	    	
	    }
	    else if(mode==1) {//1A1D
	        uI2cmAttribute.ubRegLen = 8 ;
			uI2cmAttribute.ubDataLen = 8 ;
	    	//MMPF_I2cm_Initialize(gsUSBXU_I2C_ID, 8, 8, I2CClockDiv); // This part depends on G0 clock. Need modify	    	
	    }
		MMPF_I2cm_Initialize(&uI2cmAttribute); // This part depends on G0 clock. Need modify
	    MMPF_I2cm_WriteReg(&uI2cmAttribute,addr, val);
	}
}

/*************************************************************************************************/
/* Read I2C Routines via UVC command                                                                     */
/*************************************************************************************************/
MMP_USHORT USB_ReadI2C(MMP_USHORT addr,MMP_UBYTE mode)
{
	MMP_USHORT I2CClockDiv = 0;
	MMP_USHORT ret = 0xFF;
    AITPS_GBL   pGBL = AITC_BASE_GBL;
#if (CUSTOMER == NKT)
	MMPF_I2CM_ATTRIBUTE uI2cmAttribute =  {MMPF_I2CM_ID_0, 0x14, 16, 8, 0, MMP_FALSE, MMP_FALSE, MMP_FALSE, MMP_FALSE, 0, 0, 0, MMPF_I2CM_SPEED_HW_250K, NULL, NULL};
#elif (CUSTOMER == SAC) || (CUSTOMER == LIT) || ((CUSTOMER == CCN)&&(MODEL == 1)) || ((CUSTOMER == LGT)&&(MODEL == 1))
	MMPF_I2CM_ATTRIBUTE uI2cmAttribute =  {MMPF_I2CM_ID_0, 0x14, 16, 8, 0, MMP_FALSE, MMP_FALSE, MMP_FALSE, MMP_FALSE, 0, 0, 3, MMPF_I2CM_SPEED_HW_250K, NULL, NULL};
#elif  (CUSTOMER == ASU)
	MMPF_I2CM_ATTRIBUTE uI2cmAttribute = {SENSOR_I2CM_ID, 0, 16, 8, 0, MMP_FALSE, MMP_FALSE, MMP_FALSE, MMP_FALSE, 0, 0, 0, MMPF_I2CM_SPEED_HW_250K, NULL, NULL};
#else
    MMPF_I2CM_ATTRIBUTE uI2cmAttribute =  {MMPF_I2CM_ID_0, 0, 8, 8, 0, MMP_FALSE, MMP_FALSE, MMP_FALSE, MMP_FALSE, 0, 0, 0, MMPF_I2CM_SPEED_HW_250K, NULL, NULL};	
#endif

	pGBL->GBL_CLK_DIS0 &= (~GBL_CLK_VI_DIS);
	pGBL->GBL_CLK_DIS1 &= (~GBL_CLK_I2C_DIS);//20110715@DongQ, for I2C read write at any time.
    MMPF_OS_Sleep_MS(3);

	I2CClockDiv = 200;//G0Clock/1000 * 512/380;
	uI2cmAttribute.ubSlaveAddr = gsUSBXU_I2C_ID;
	
	if((gsUSBXU_I2C_ID > 0) && ((pGBL->GBL_CLK_DIS0 & GBL_CLK_VI_DIS) == 0x0)){
		if(mode==0){ //2A1D
			uI2cmAttribute.ubRegLen = 16 ;
			uI2cmAttribute.ubDataLen = 8 ;	    	
	    }
	    else if(mode==1) {//1A1D
	        uI2cmAttribute.ubRegLen = 8 ;
			uI2cmAttribute.ubDataLen = 8 ;
	    	//MMPF_I2cm_Initialize(gsUSBXU_I2C_ID, 8, 8, I2CClockDiv); // This part depends on G0 clock. Need modify	    	
	    }
		MMPF_I2cm_Initialize(&uI2cmAttribute);
	    MMPF_I2cm_ReadReg(&uI2cmAttribute,addr, &ret);
	}
	
	return ret;
}
#endif

#if CAPTURE_BAYER_RAW_ENABLE
PCAM_BAYER_INFO *USB_GetBayerRawInfo(void)
{
static PCAM_BAYER_INFO gsBayerRawInfo = 
{
#if BIND_SENSOR_MT9T002==1
    ( BAYER_10BPP | BAYER_PACKED | BAYER_RGBORDER(1) ),
    2304,1536
//#endif
#elif BIND_SENSOR_OV2710==1
    ( BAYER_10BPP | BAYER_PACKED | BAYER_RGBORDER(3) ),
    1928,1092
//#endif
#elif BIND_SENSOR_S5K5B3GX==1
    ( BAYER_10BPP | BAYER_PACKED | BAYER_RGBORDER(3) ),
    1928,1092
#else
    0
#endif
} ;
    return &gsBayerRawInfo ;    
}
#endif

//
// Using XU to call this function to enable Bayer Raw preview
// By YUY2 index
//
void USB_EnableBayerRawPreview(MMP_BOOL enable)
{
    gbBayerRawPreviewEn = enable;
}

MMP_BOOL USB_IsBayerRawPreview(void)
{
    return gbBayerRawPreviewEn ;
}
